﻿package fpc.controls.buttons;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.MouseEvent;
import fpc.AComponent;
import fpc.styles.skins.ButtonState;
import fpc.styles.skins.IButtonSkin;
import fpc.utils.Herald;

class AButton extends AComponent, implements IButton
{
	public var heraldClick(default, null):Herald<IButton>;
	
	var _over:Bool;
	
	var _down:Bool;
	
	public function new() 
	{
		heraldClick = new Herald();
		
		_over = false;
		_down = false;
		_selected = false;
		_toggle = false;
		_enabled = true;
		
		super();
		
		applyListenersEnabled();
	}
	
	var _sprite:Sprite;
	
	override function createBaseObject():DisplayObject
	{
		_sprite = new Sprite();
		return _sprite;
	}
	
	function onMouseOver(event:MouseEvent)
	{
		_over = true;
		_buttonSkin.state = OVER;
		orderRender();
		_sprite.addEventListener(MouseEvent.ROLL_OUT, onMouseOut);
	}
	
	function onMouseOut(event:MouseEvent)
	{
		_over = false;
		
		if (!_enabled)
		{
			return;
		}
		
		if(!_down)
		{
			_buttonSkin.state = NORMAL;
			orderRender();
		}
	}
	
	function onMouseDown(event:MouseEvent)
	{
		_down = true;
		_buttonSkin.state = DOWN;
		orderRender();
		_sprite.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
	}
	
	function onMouseUp(event:MouseEvent)
	{
		_sprite.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		_down = false;
		
		if (!_enabled)
		{
			return;
		}
		
		if (_over && _toggle)
		{
			selected = !_selected;
		}
		
		_buttonSkin.state = _over ? OVER : NORMAL;
		orderRender();
		
		if (_over)
		{
			heraldClick.dispatch(this);
		}
	}
	
	public var selected(getSelected, setSelected):Bool;
	var _selected:Bool;
	function setSelected(value:Bool)
	{
		if (!_toggle)
		{
			return value;
		}
		
		_selected = value;
		
		_buttonSkin.selected = value;
		orderRender();
		return value;
	}
	function getSelected()
	{
		return _selected;
	}
	
	public var toggle(getToggle, setToggle):Bool;
	var _toggle:Bool;
	function getToggle()
	{
		return _toggle;
	}
	function setToggle(value:Bool)
	{
		_toggle = value;
		return value;
	}
	
	public var enabled(getEnabled, setEnabled):Bool;
	var _enabled:Bool;
	function getEnabled()
	{
		return _enabled;
	}
	function setEnabled(value)
	{
		if (value == _enabled)
		{
			return value;
		}
		
		_enabled = value;
		
		switch (_buttonSkin.state)
		{
			case NORMAL, OVER, DOWN:
				if (!_enabled)
				{
					_buttonSkin.state = DISABLED;
				}
			case DISABLED:
				if (_enabled)
				{
					_buttonSkin.state = NORMAL;
				}
		}
		
		applyListenersEnabled();
		orderRender();
		return value;
	}
	
	function applyListenersEnabled()
	{
		if (_enabled)
		{
			_sprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			_sprite.addEventListener(MouseEvent.ROLL_OVER, onMouseOver);
		}
		else
		{
			_sprite.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			_sprite.removeEventListener(MouseEvent.ROLL_OVER, onMouseOver);
		}
	}
	
	var _buttonSkin:IButtonSkin;
	
	function applyButtonSkin(value:IButtonSkin)
	{
		if (_buttonSkin != null)
		{
			_sprite.removeChild(_buttonSkin.object);
		}
		
		_buttonSkin = value;
		
		value.state = _enabled ? NORMAL : DISABLED;
		value.selected = _selected;
		
		_sprite.addChild(value.object);
		
		tryResize(_width, _height);
		
		orderRender();
	}
	
	function tryResize(width:Float, height:Float)
	{
		
	}
}